Function.RegisterNamespace("System.IO.FileStrategy");

System.IO.FileStrategy.FileSystemObject=function(){
    // Private Members
    var _unicodeRegex=/[^\u0000-\u00ff]/;

    // IFileStrategy Members
    this.DeleteFile=function(path){
        var fso=new ActiveXObject("Scripting.FileSystemObject");
        return fso.DeleteFile(path,true);
    };

    this.Exists=function(path){
        var fso=new ActiveXObject("Scripting.FileSystemObject");
        return fso.FileExists(path);
    };

    this.GetFile=function(path){
        var text=null;
        var fso=new ActiveXObject("Scripting.FileSystemObject");
        var file=fso.FileExists(path)&&fso.GetFile(path);
        if(file){
            var stream=file.OpenAsTextStream(1);
            var BOM='';
            try{
                if(!stream.AtEndOfStream)BOM=stream.Read(3);
            }catch(e){
                if(!stream.AtEndOfStream)BOM=stream.ReadAll();
            }
            switch(BOM.charCodeAt(0)){
                case 0xFF:
                    if(BOM.charCodeAt(1)==0xFE){ 
                        //UTF-16 little-endian
                        stream.Close();
                        stream=file.OpenAsTextStream(1,-1);
                    }
                    break;
                case 0xFE:
                    if(BOM.charCodeAt(1)==0xFF){ 
                        //UTF-16 big-endian
                        stream.Close();
                        stream=file.OpenAsTextStream(1,-1);
                    }
                    break;
                case 0xEF:
                    if(BOM.charCodeAt(1)==0xBB&&BOM.charCodeAt(2)==0xBF){ 
                        //UTF-8 BOM
                        stream.Close();
                        stream=new ActiveXObject("ADODB.Stream");
                        stream.CharSet="UTF-8";
                        stream.Open();
                        stream.LoadFromFile(path);
                        text=stream.ReadText();
                        break;
                    }
                default: 
                    // Unkown BOM or BOM not present
                    text=BOM;
                    break;
            }
            if(typeof(stream.ReadAll)!="undefined"&&!stream.AtEndOfStream){
                text=(text||'')+stream.ReadAll();
            }
            stream.Close();
        }
        return text;
    };
        
    this.SaveFile=function(path,text){
        var fso=new ActiveXObject("Scripting.FileSystemObject");
        var file=fso.CreateTextFile(path, true, _unicodeRegex.test(text));
        file.Write(text);
        file.Close();
    };

    // IStrategySpecification Members
    this.IsSatisfiedBy=function(candidate){
        return typeof(ActiveXObject)!="undefined" && typeof(WScript) !="undefined";
    };
    
};

System.IO.FileStrategy.FileSystemObject.Implement(System.IO.FileStrategy.IFileStrategy,'System.IO.FileStrategy.FileSystemObject');
System.IO.FileStrategy.FileSystemObject.Implement(System.Script.Strategy.IStrategySpecification,'System.IO.FileStrategy.FileSystemObject');

System.IO.File.Strategies.Add(System.IO.FileStrategy.FileSystemObject);